package com.free.framework.base;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.free.framework.R;
import com.free.framework.mvp.BaseMvpPresenter;
import com.free.framework.mvp.IBaseMvpView;
import com.free.framework.ui.DlgFactory;
import com.free.framework.ui.FjrAppbar;
import com.free.framework.ui.IAppbar;
import com.free.framework.widgets.custom_loading.LoadingUtils;

/**
 * @author 李博
 * @date 16/6/13
 * @desc BaseActivity
 */
public abstract class BaseActivity<P extends BaseMvpPresenter> extends AppCompatActivity implements IBaseMvpView {

    protected Dialog mProgressDialog;
    protected IAppbar mFjrAppbar;
    protected P mPresenter;

    @CallSuper
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //最终方案
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏 为了适配有虚拟按键的手机，不要打开这项
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        super.onCreate(savedInstanceState);
    }

    /**
     * @desc 不再保存Fragment的状态，达到fragment随MyActivity一起销毁
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        if (null != mFjrAppbar) {
            mFjrAppbar.attachToActivity(this);
        }

        mPresenter = createPresenter();
        if (null != mPresenter) {
            mPresenter.attachView(this);
            onPresenterCreated();
        }
    }


    protected void setAppbar(IAppbar appbar) {
        this.mFjrAppbar = appbar;
    }

    protected void setAppbar(@StringRes int tilte) {
        this.mFjrAppbar = new FjrAppbar.Builder(this)
                .leftImgResId(R.mipmap.back)
                .setLeftClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                })
                .title(tilte)
                .create();
    }

    /**
     * @return
     * @desc 创建Presenter
     */
    protected P createPresenter() {
        return null;
    }

    /**
     * @desc Presenter创建完成
     */
    protected void onPresenterCreated() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != mPresenter) {
            mPresenter.resume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != mPresenter) {
            mPresenter.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.destroy();
        }
    }

    @Override
    public void showToast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoadingView(boolean cancelable) {
//        if (mProgressDialog == null) {
//            mProgressDialog = DlgFactory.createProgressDlg(this);
//        }
//        if (!mProgressDialog.isShowing()) {
//            mProgressDialog.show();
//        }
        LoadingUtils.showDialog(getSupportFragmentManager());

    }

    @Override
    public void hideLoadingView() {
//        if (mProgressDialog != null && mProgressDialog.isShowing()) {
//            mProgressDialog.dismiss();
//        }
        LoadingUtils.dismiss(getSupportFragmentManager());
    }

    @Override
    public void hideRefreshingView() {

    }

    @Override
    public void showSSLError() {
        Toast.makeText(this, "证书校验失败", Toast.LENGTH_SHORT).show();
    }


}
